#ifndef __PROCESSPOOL_HPP__
#define __PROCESSPOOL_HPP__

#include "Task.hpp"
#include <iostream>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <cstdio>
#include <fcntl.h>
class Channel
{
public:
    Channel(int wfd, pid_t subid) : _wfd(wfd), _subid(subid)
    {
        _name = "Channel[" + std::to_string(wfd) + "]-进程[" + std::to_string(subid) + "]";
    }

    std::string Name() { return _name; }

    int Fd() { return _wfd; }

    pid_t SubId() { return _subid; }

    void Sent(int code)
    {
        ssize_t n = write(_wfd, &code, sizeof(code));
        (void)n;
    }

    void Close()
    {
        close(_wfd);
    }

    void Wait()
    {
        pid_t subid = waitpid(_subid, nullptr, 0);
        (void)subid;
    }

    ~Channel()
    {
    }

private:
    int _wfd;
    pid_t _subid;
    std::string _name;
};

class ChannelManager
{
public:
    ChannelManager() : _next(0)
    {
    }

    void Insert(int wfd, pid_t subid)
    {
        _channels.emplace_back(wfd, subid);
    }

    Channel &Select()
    {
        auto &c = _channels[_next];
        _next++;
        _next %= _channels.size();
        return c;
    }

    void StopSubProcess()
    {
        for (auto &channel : _channels)
        {
            channel.Close();
            std::cout << "管道[" << channel.Name() << "]关闭" << std::endl;
        }
    }

    void WaiSubProcess()
    {
        for (auto &channel : _channels)
        {
            channel.Wait();
            std::cout << "管道[" << channel.Name() << "]回收" << std::endl;

        }
    }

    ~ChannelManager()
    {
    }

private:
    std::vector<Channel> _channels;
    int _next;
};

const int gdefaultnum = 5;

class ProcessPool
{
public:
    ProcessPool() :_process_num(gdefaultnum)
    {
        _tm.Register(PrintLog);
        _tm.Register(DownLoad);
        _tm.Register(UpLoad);
    }

    void Work(int rfd)
    {
        while(true)
        {
            int code = 0;
            ssize_t n = read(rfd, &code, sizeof(code));
            if(n > 0)
            {
                if(n != sizeof(code))
                    continue;
                std::cout << "子进程[" << getpid() << "]得到了一个任务码" << std::endl;
                _tm.Excute(code);
            }
            else if(n == 0)
            {
                std::cout << "进程[" << getpid() << "]退出" << std::endl;
                break;
            }
            else
            {
                std::cout << "read异常" << std::endl;
                break;
            }
        }
    }

    bool Start()
    {
        for(int i = 0; i < _process_num; i++)
        {
            int pipefd[2] = {0};
            int n = pipe(pipefd);
            if(n < 0) return false;
            pid_t subid = fork();
            if(subid < 0) return false;
            else if(subid == 0)
            {
                close(pipefd[1]);
                Work(pipefd[0]);
                close(pipefd[0]);
                exit(0);
            }
            else
            {
                close(pipefd[0]);
                _cm.Insert(pipefd[1], subid);
            }
        }
        return true;
    }

    void Run()
    {
        int taskcode = _tm.Code();
        auto& c = _cm.Select();
        c.Sent(taskcode);
    }

    void Stop()
    {
        _cm.StopSubProcess();
        _cm.WaiSubProcess();
    }

    ~ProcessPool()
    {}
private:
    ChannelManager _cm;
    TaskManager _tm;
    int _process_num;
};

#endif